<template>
	<view class="container">
		<view class="header">
			<view class="header-left" @tap="goBack">
				<text class="back-icon">&#xe600;</text>
			</view>
			<text class="title">我的消息</text>
			<view class="header-right"></view>
		</view>

		<!-- 消息列表 -->
		<scroll-view scroll-y class="list">
			<view v-for="(message, index) in messages" :key="message.id || index" class="message-card" @tap="viewMessageDetail(message)">
				<view class="avatar-container">
					<view class="avatar-circle">
						<text class="avatar-text">{{ message.title.charAt(0) }}</text>
						<view v-if="!message.isRead" class="unread-badge"></view>
					</view>
				</view>
				<view class="content">
					<view class="header-row">
						<text class="sender">{{ message.title }}</text>
						<text class="time">{{ message.time }}</text>
					</view>
					<text class="preview">{{ message.preview }}</text>
				</view>
			</view>
			<view v-if="messages.length === 0" class="empty">暂无消息</view>
		</scroll-view>
	</view>
</template>

<script lang="ts" setup>
	import { ref, onMounted } from 'vue';
	
	const goBack = () => {
		uni.navigateBack();
	};

	const messages = ref<any[]>([]);

	// 获取所有消息
	const fetchMessages = () => {
		uni.request({
			url: 'http://localhost:9003/message/getMessage',
			data: {
				toUser: uni.getStorageSync('userId') || 22
			},
			header: {
				'custom-header': 'application/json'
			},
			success: (res) => {
				console.log('获取消息列表成功', res);
				const data = res.data || [];
				messages.value = data.map(msg => ({
					id: msg.messageid,
					title: `用户${msg.fromUser}`,
					time: formatMessageTime(msg.sendTime),
					preview: msg.content,
					isRead: msg.isRead || false
				}));
			},
			fail(err) {
				console.error('获取消息列表失败', err);
				uni.showToast({
					title: '获取消息列表失败',
					icon: 'none'
				});
			}
		});
	};

	// 格式化消息时间显示
	const formatMessageTime = (timeString) => {
		if (!timeString) return '';
		const date = new Date(timeString);
		const now = new Date();
		
		// 如果是今天的消息，只显示时间
		if (date.toDateString() === now.toDateString()) {
			return date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });
		}
		
		// 如果是昨天或更早，显示日期
		return date.toLocaleDateString('zh-CN', { month: 'short', day: 'numeric' });
	};

	// 查看消息详情
	const viewMessageDetail = (message) => {
		uni.showToast({
			title: message.preview,
			icon: 'none',
			duration: 3000
		});
		
		// 标记为已读
		if (!message.isRead) {
			message.isRead = true;
		}
	};

	onMounted(() => {
		fetchMessages();
	});
</script>

<style>
	.container { 
		padding: 30rpx; 
		min-height: 100vh;
		background-color: #f5f5f5;
	}
	
	@font-face {
		font-family: 'iconfont';
		src: url('https://at.alicdn.com/t/font_2828393_4pue5j9m4q.woff2?t=1636602756377') format('woff2'),
			url('https://at.alicdn.com/t/font_2828393_4pue5j9m4q.woff?t=1636602756377') format('woff'),
			url('https://at.alicdn.com/t/font_2828393_4pue5j9m4q.ttf?t=1636602756377') format('truetype');
	}

	.iconfont {
		font-family: "iconfont" !important;
		font-size: 16px;
		font-style: normal;
		-webkit-font-smoothing: antialiased;
		-moz-osx-font-smoothing: grayscale;
	}
	
	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		height: 88rpx;
		background: #1976D2;
		padding: 0 20rpx;
		position: relative;
	}
	
	.header-left, .header-right {
		width: 80rpx;
		height: 88rpx;
		display: flex;
		align-items: center;
	}
	
	.back-icon {
		font-family: "iconfont" !important;
		color: #fff;
		font-size: 36rpx;
	}
	
	.title { 
		font-size: 36rpx; 
		font-weight: 700; 
		color: #fff;
	}
	
	.list { 
		max-height: calc(100vh - 200rpx); 
	}
	
	.message-card { 
		display: flex; 
		gap: 20rpx; 
		background: #fff; 
		padding: 24rpx 20rpx;
		border-bottom: 1rpx solid #eee;
		transition: all 0.3s ease;
	}
	
	.message-card:active {
		background-color: #f9f9f9;
	}
	
	.avatar-container {
		display: flex;
		align-items: center;
		justify-content: center;
		flex-shrink: 0;
	}
	
	.avatar-circle {
		width: 96rpx;
		height: 96rpx;
		border-radius: 50%;
		background: #1976D2;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
	}
	
	.avatar-text {
		color: #ffffff;
		font-size: 28rpx;
		font-weight: 600;
	}
	
	.content {
		flex: 1;
		display: flex;
		flex-direction: column;
		gap: 10rpx;
		min-width: 0;
	}
	
	.header-row {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	
	.sender {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}
	
	.time {
		font-size: 24rpx;
		color: #999;
		flex-shrink: 0;
		margin-left: 20rpx;
	}
	
	.preview {
		font-size: 24rpx;
		color: #666;
		line-height: 1.4;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;
		overflow: hidden;
	}
	
	.status {
		display: flex;
		align-items: center;
		padding-left: 10rpx;
	}
	
	.unread-badge {
		position: absolute;
		top: 0;
		right: 0;
		width: 20rpx;
		height: 20rpx;
		background: #ff3333;
		border-radius: 50%;
		border: 4rpx solid #fff;
	}
	
	.empty {
		text-align: center;
		color: #999;
		margin-top: 100rpx;
		font-size: 28rpx;
	}
</style>